"use client"

import { useState } from "react"
import Image from "next/image"
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { Plus, Pencil, X } from 'lucide-react'

const initialProducts = [
  {
    id: 1,
    name: "大师风T恤",
    price: 700,
    stock: 10,
    status: "在售",
    image: "/images/tshirt.jpg",
    category: "上衣",
  },
  {
    id: 2,
    name: "经典盔甲卫衣",
    price: 620,
    stock: 5,
    status: "在售",
    image: "/images/盔甲.jpg",
    category: "上衣",
  },
  {
    id: 3,
    name: "超美仙气裙",
    price: 850,
    stock: 4,
    status: "下架",
    image: "/images/裙子.jpg",
    category: "裤子",
  },
]

export default function ProductsPage() {
  const [products, setProducts] = useState(initialProducts)
  const [isAddDialogOpen, setIsAddDialogOpen] = useState(false)
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false)
  const [isRenameDialogOpen, setIsRenameDialogOpen] = useState(false)
  const [productToDelete, setProductToDelete] = useState(null)
  const [productToRename, setProductToRename] = useState(null)
  const [newProductName, setNewProductName] = useState("")

  const handleDeleteProduct = (product) => {
    setProductToDelete(product)
    setIsDeleteDialogOpen(true)
  }

  const confirmDeleteProduct = () => {
    setProducts(products.filter(p => p.id !== productToDelete.id))
    setIsDeleteDialogOpen(false)
  }

  const handleRenameProduct = (product) => {
    setProductToRename(product)
    setNewProductName(product.name)
    setIsRenameDialogOpen(true)
  }

  const confirmRenameProduct = () => {
    setProducts(products.map(p =>
      p.id === productToRename.id ? { ...p, name: newProductName } : p
    ))
    setIsRenameDialogOpen(false)
  }

  return (
    <div className="p-4 sm:p-6 lg:p-8 space-y-4 sm:space-y-6">
      <div className="flex justify-between items-center">
        <h1 className="text-2xl font-bold">商品管理</h1>
        <Dialog open={isAddDialogOpen} onOpenChange={setIsAddDialogOpen}>
          <DialogTrigger asChild>
            <Button className="bg-amber-500 hover:bg-amber-600">
              <Plus className="mr-2 h-4 w-4" />
              添加商品
            </Button>
          </DialogTrigger>
          <DialogContent className="sm:max-w-[425px] max-h-[90vh] overflow-y-auto">
            <DialogHeader>
              <DialogTitle>添加新商品</DialogTitle>
              <DialogDescription>
                请填写新商品的详细信息
              </DialogDescription>
            </DialogHeader>
            <div className="grid gap-4 py-4">
              <div className="grid gap-2">
                <Label htmlFor="name">商品名称</Label>
                <Input id="name" />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="price">价格</Label>
                <Input id="price" type="number" />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="stock">库存</Label>
                <Input id="stock" type="number" />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="description">商品描述</Label>
                <Textarea id="description" />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="image">商品图片</Label>
                <Input id="image" type="file" />
              </div>
            </div>
            <DialogFooter>
              <Button variant="outline" onClick={() => setIsAddDialogOpen(false)}>
                取消
              </Button>
              <Button className="bg-amber-500 hover:bg-amber-600">
                确认添加
              </Button>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      </div>

      <Card>
        <div className="overflow-x-auto">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead className="w-[100px]">商品图片</TableHead>
                <TableHead>商品名称</TableHead>
                <TableHead>价格</TableHead>
                <TableHead className="hidden sm:table-cell">库存</TableHead>
                <TableHead className="hidden sm:table-cell">状态</TableHead>
                <TableHead className="text-right">操作</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {products.map((product) => (
                <TableRow key={product.id}>
                  <TableCell>
                    <Image
                      src={product.image}
                      alt={product.name}
                      width={50}
                      height={50}
                      className="rounded-lg"
                    />
                  </TableCell>
                  <TableCell className="font-medium">{product.name}</TableCell>
                  <TableCell>¥{product.price}</TableCell>
                  <TableCell className="hidden sm:table-cell">{product.stock}</TableCell>
                  <TableCell className="hidden sm:table-cell">
                    <span
                      className={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium ${product.status === "在售"
                        ? "bg-green-100 text-green-800"
                        : "bg-gray-100 text-gray-800"
                        }`}
                    >
                      {product.status}
                    </span>
                  </TableCell>
                  <TableCell className="text-right">
                    <div className="flex justify-end gap-2">
                      <Button variant="outline" size="icon" onClick={() => handleRenameProduct(product)}>
                        <Pencil className="h-4 w-4" />
                      </Button>
                      <Button variant="outline" size="icon" className="text-red-500 hover:text-red-600" onClick={() => handleDeleteProduct(product)}>
                        <X className="h-4 w-4" />
                      </Button>
                    </div>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      </Card>

      <Dialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>确认删除商品</DialogTitle>
            <DialogDescription>
              您确定要删除商品 "{productToDelete?.name}" 吗？此操作不可撤销。
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button variant="outline" onClick={() => setIsDeleteDialogOpen(false)}>取消</Button>
            <Button variant="destructive" onClick={confirmDeleteProduct}>确认删除</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>

      <Dialog open={isRenameDialogOpen} onOpenChange={setIsRenameDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>重命名商品</DialogTitle>
            <DialogDescription>
              请输入 "{productToRename?.name}" 的新名称
            </DialogDescription>
          </DialogHeader>
          <div className="grid gap-4 py-4">
            <div className="grid gap-2">
              <Label htmlFor="newName">新名称</Label>
              <Input
                id="newName"
                value={newProductName}
                onChange={(e) => setNewProductName(e.target.value)}
              />
            </div>
          </div>
          <DialogFooter>
            <Button variant="outline" onClick={() => setIsRenameDialogOpen(false)}>取消</Button>
            <Button onClick={confirmRenameProduct}>确认重命名</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  )
}

